package com.abc.controller;


import com.abc.entity.DailyRecord;
import com.abc.service.IDailyRecordService;
import com.abc.service.IUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/dailyRecord")
@Api(tags = "日常记录管理")
public class DailyRecordController {
    @Autowired
    private IDailyRecordService dailyRecordService;

    @Autowired
    private IUserService userService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID获取日常记录", notes = "传入日常记录ID")
    public DailyRecord getDailyRecordById(@PathVariable Long id) {
        return dailyRecordService.getById(id);
    }

    @PostMapping
    @ApiOperation(value = "添加日常记录", notes = "传入日常记录详情")
    public boolean addDailyRecord(@RequestBody DailyRecord dailyRecord) {
        boolean b = userService.checkUser(dailyRecord.getUserId());
        if (!b) {
            throw new RuntimeException("用户不存在");
        }
        return dailyRecordService.save(dailyRecord);
    }

    @PutMapping
    @ApiOperation(value = "更新日常记录", notes = "传入日常记录详情")
    public boolean updateDailyRecord(@RequestBody DailyRecord dailyRecord) {
        return dailyRecordService.updateById(dailyRecord);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除日常记录", notes = "传入日常记录ID")
    public boolean deleteDailyRecord(@PathVariable Long id) {
        return dailyRecordService.removeById(id);
    }
}
